package com.winit.requirement.manager;

import com.winit.commons.util.SeqGenerate;
import com.winit.requirement.model.ReqNotice;
import com.winit.requirement.repository.ReqNoticeRepository;
import com.winit.requirement.repository.ReqNoticeRepositoryExt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.List;

@Component
public class ReqNoticeManager {
    private static Logger log = LoggerFactory.getLogger(ReqNoticeManager.class);

    @Autowired
    private ReqNoticeRepository reqNoticeRepository;
    @Autowired
    private ReqNoticeRepositoryExt reqNoticeRepositoryExt;


    public ReqNotice createReqNotice(ReqNotice reqNotice){
        if (StringUtils.isEmpty(reqNotice.getId())){
            reqNotice.setId(SeqGenerate.createId("RNC"));
        }
        if (StringUtils.isEmpty(reqNotice.getNumber())){
            reqNotice.setNumber("RNC" + SeqGenerate.createSerialNo());
        }
        this.reqNoticeRepository.save(reqNotice);
        return reqNotice;
    }


    public ReqNotice updateReqNotice(ReqNotice reqNotice){
        this.reqNoticeRepositoryExt.updatePropertiesNotNullById(reqNotice);
        return reqNotice;
    }

    public List<ReqNotice> getReqNotices(String id, String number, String reqId){
        return this.reqNoticeRepository.findByProperties(id, number, reqId);
    }


    public void deleteReqNotice(String id){
        if (null != this.reqNoticeRepository.findOne(id)) {
            this.reqNoticeRepository.delete(id);
        }
    }

}
